<template>
    <div class="main">
      <div class="login-container">
        <el-row justify="center" align="middle" class="login-box">
          <el-col :span="8">
            <h2 class="title">登录到Chat</h2>
            <el-form ref="loginForm" :model="form" :rules="rules" label-position="top" label-width="80px">
              <el-form-item label="用户名" prop="username">
                <el-input v-model="form.username" class="login-input" @keyup.enter=login></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="form.password" class="login-input" @keyup.enter=login></el-input>
              </el-form-item>
              <el-form-item label="验证码" prop="verificationCode">
                <el-input v-model="form.verificationCode" class="login-input" @keyup.enter=login></el-input>
              </el-form-item>
              <el-form-item>
                <img :src="imageSrc" alt="验证码" @click="refreshCode" class="verification-code">
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="login" class="login-btn">登录</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
</template>

<script>
import axiosInstance from '@/api/request'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
        verificationCode: '',
        codeKey: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        verificationCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      imageSrc: ''
    }
  },
  methods: {
    async fetchVerificationCode() {
      try {
        const response = await axiosInstance.post('/verification/getCode')
        this.form.codeKey = response.data.data.codeKey
        this.imageSrc = 'data:image/png;base64,' + response.data.data.base64Image
      } catch (error) {
        console.error(error)
      }
    },
    refreshCode() {
      this.fetchVerificationCode()
    },
    async login() {
      try {
        await this.$refs.loginForm.validate()
        const response = await axiosInstance.post('/user/login', this.form)
        if (response.data.state.code === '0'){
          localStorage.setItem('token', response.data.data.token)
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          this.$router.push('/chat')
        }else{
          this.$message({
            message: response.data.state.msg,
            type: 'error'
          })
          this.refreshCode()
        }
      } catch (error) {
        console.error(error)
        this.refreshCode()
        this.$message({
          message: '登录失败，请检查用户名、密码和验证码是否正确',
          type: 'error'
        })
      }
    }
  },
  mounted() {
    this.fetchVerificationCode()
  }
}
</script>

<style scoped>
.login-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  padding: 24px;
  border-radius: 8px;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.1);
  padding: 20px;
  border-radius: 4px;
}


.title {
  color: antiquewhite;
  font-size: 20px;
  text-align: center;
  margin-bottom: 20px;
}

.verification-code {
  width: 100%;
  cursor: pointer;
}

.login-input {
  width: 360px;
}
.login-btn {
  margin-left: 140px;
}
.main {
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../assets/background.jpg");
  background-size: contain;
  background-color: #f5f5f5;
  min-height: 100vh; /* 修改此处 */
}
</style>